package com.bawei.image.loader

import android.content.Context
import android.net.Uri
import android.widget.ImageView
import com.bawei.image.option.ImageOptions
import com.bawei.image.skill.IImageLoader
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import java.io.File
import java.lang.IllegalArgumentException
import java.lang.ref.SoftReference

/**
 * Author: Jason Zhang
 * Date: 2024/4/23
 * Desc: Glide图片加载器
 */
class GlideImgLoader : IImageLoader {

    override fun loadFileIntoImageView(
        context: Context,
        file: File,
        target: ImageView,
        options: ImageOptions
    ) {
        loadImageWithGlide(context, file, target, options)
    }

    override fun loadResourceIntoImageView(
        context: Context,
        resId: Int,
        target: ImageView,
        options: ImageOptions
    ) {
        loadImageWithGlide(context, resId, target, options)
    }

    override fun loadUriIntoImageView(
        context: Context,
        uri: Uri,
        target: ImageView,
        options: ImageOptions
    ) {
        loadImageWithGlide(context, uri, target, options)
    }

    override fun loadUrlIntoImageView(
        context: Context,
        url: String,
        target: ImageView,
        options: ImageOptions
    ) {
        loadImageWithGlide(context, url, target, options)
    }

    override fun loadByteArrayIntoImageView(
        context: Context,
        bytes: ByteArray,
        target: ImageView,
        options: ImageOptions
    ) {
        loadImageWithGlide(context, bytes, target, options)
    }

    private fun loadImageWithGlide(
        context: Context,
        source: Any,
        target: ImageView,
        options: ImageOptions
    ) {
        var softRef = SoftReference(context)
        softRef.get()?.apply {
            var requestManager = Glide.with(this)

            //是否加载静态图片(gif->png)
            if (options.isGif && options.isStaticImg) {
                requestManager.asBitmap()
            }

            var requestBuilder = requestManager.load(source)
            //设置占位图片
            if (options.placeImage > 0) {
                requestBuilder.placeholder(options.placeImage)
            }
            //设置错误占位图
            if (options.errorImage > 0) {
                requestBuilder.error(options.errorImage)
            }
            //是否需要跳过内存缓存
            if (options.skipMemoryCache) {
                requestBuilder.skipMemoryCache(true)
            }
            //设置磁盘缓存策略
            if (options.diskCacheStrategy != DiskCacheStrategy.NONE) {
                requestBuilder.diskCacheStrategy(options.diskCacheStrategy)
            }
            //设置图片大小
            if (options.imgSize.size == 2) {
                requestBuilder.submit(options.imgSize[0], options.imgSize[1])
            }
            //设置圆形图片
            if (options.isCircle) {
                requestBuilder.transform(CenterCrop(), CircleCrop())
            }
            //设置圆角图片
            if (options.roundRadius > 0) {
                requestBuilder.transform(CenterCrop(), RoundedCorners(options.roundRadius))
            }

            //将获取到图片设置到目标ImageView
            requestBuilder.into(target)

        }
    }
}